<form action="cart.php" method="get">
	<table class="cart table-stripped table-hover">
		<!-- Header of the cart -->
		<thead>
			<tr><th colspan="4">Votre panier</th></tr>
			<tr>
				<th>Item</th>
				<th style='width:100px;'>Quantité</th>
				<th style='width:200px;'>Total</th>
				<th style='width:100px;'></th>
			</tr>
		</thead>
		
		<!-- Body of the cart : articles description-->
		<tbody id='cart'>
			<?php
			include_once '../modele/OrderItem.php';
			include_once '../modele/OrderTopping.php';
			include_once '../modele/Side.php';
			include_once '../modele/Drink.php';
			if(!isset($_SESSION['order_id'])){
				$_SESSION['order_id'] = -1;
			}
			$orderItems = OrderItem::dbGetOrderItems("order_id = ".$_SESSION['order_id']);
			
			$totalCash = 0;
			$totalPts = 0;
			$p = 0;
			$pizzas = array();
			$s = 0;
			$sides = array();
			$d = 0;
			$drinks = array();

			foreach ($orderItems as $cartItem) {
				if($cartItem->getType() == 'P'){
					$pizzas[$p] = $cartItem;
					$p++;
				}else if($cartItem->getType() == 'D'){
					$drinks[$d] = $cartItem;
					$d++;
				}else if($cartItem->getType() == 'S'){
					$sides[$s] = $cartItem;
					$s++;
				}
			}

			foreach($pizzas as $pizza){
				$price = 0;
				$pts = 0;
				$pizTopping = OrderTopping::dbGetOrderToppings("order_item_id = ".$pizza->getId());

				if($pizza->getScore()){
					if($pizza->getSize() == 'P'){
						$pts += 4000;
					}
					if($pizza->getSize() == 'M'){
						$pts += 6000;
					}
					if($pizza->getSize() == 'L'){
						$pts += 7500;
					}
					foreach($pizTopping as $top){
						$pts += ($top->getQuantity()*250);
					}
				}else{
					if($pizza->getSize() == 'P'){
						$price += 8.00;
					}
					if($pizza->getSize() == 'M'){
						$price += 11.00;
					}
					if($pizza->getSize() == 'L'){
						$price += 14.00;
					}
					foreach($pizTopping as $top){
						$price += ($top->getQuantity()*0.5);
					}
				}

				$name = $pizza->getNameOrId();
			 	$quantity = $pizza->getQuantity();

			 	echo "<tr>".
			 		"<td>".$name."</td>".
			 		"<td style=\"width:100px;\"><input type='text' name='quantity' value='".$quantity."' data-provide='typeahead' class='input-mini'></td>".
			 		"<td style=\"width:200px;\">";
					
					if($pizza->getScore()){
						echo "$pts Pts";
					}else{
						echo number_format($price, 2, ',', ' ')."$";
					}
					
			 		echo "</td><td style=\"width:100px;\"><input type='submit' name='delete' value='Supprimer' class='btn'>".
			 		"</td>".
			             "</tr>";

				$totalCash += ($price*$quantity);
				$totalPts += $pts;
			}
			
			foreach($sides as $side){
				$price = 0;
				$pts = 0;
				$si = Side::dbGetSide("name = '".$side->getNameOrId()."'");

				foreach($si as $sid){
					if($side->getScore()){
						if($side->getSize() == 'P'){
							$pts = $sid->getScoreSmall();
						}
						if($side->getSize() == 'M'){
							$pts = $sid->getScoreMedium();
						}
						if($side->getSize() == 'L'){
							$pts = $sid->getScoreLarge();
						}
					}else{
						if($side->getSize() == 'P'){
							$price = $sid->getPriceSmall();
						}
						if($side->getSize() == 'M'){
							$price = $sid->getPriceMedium();
						}
						if($side->getSize() == 'L'){
							$price = $sid->getPriceLarge();
						}
					}
				}
				$name = $side->getNameOrId();
			 	$quantity = $side->getQuantity();

			 	echo "<tr>".
			 		"<td>".$name."</td>".
			 		"<td style=\"width:100px;\"><input type='text' name='quantity' value='".$quantity."' data-provide='typeahead' class='input-mini'></td>".
			 		"<td style=\"width:200px;\">";
					
					if($side->getScore()){
						echo "$pts Pts";
					}else{
						echo number_format($price, 2, ',', ' ')."$";
					}
					
					echo "</td>".
			 		"<td style=\"width:100px;\"><input type='submit' name='delete' value='Supprimer' class='btn'>".
			 		"</td>".
			             "</tr>";

				$totalCash += ($price*$quantity);
				$totalPts += $pts;
			}

			foreach($drinks as $drink){
				$price = 0;
				$pts = 0;
				$si = Drink::dbGetDrinks("name = '".$drink->getNameOrId()."'");
				
				foreach($si as $sid){
					if($drink->getScore()){
						if($drink->getSize() == 'P'){
							$pts = $sid->getScoreSmall();
						}
						if($drink->getSize() == 'M'){
							$pts = $sid->getScoreMedium();
						}
						if($drink->getSize() == 'L'){
							$pts = $sid->getScoreLarge();
						}
					}else{
						if($drink->getSize() == 'P'){
							$price = $sid->getPriceSmall();
						}
						if($drink->getSize() == 'M'){
							$price = $sid->getPriceMedium();
						}
						if($drink->getSize() == 'L'){
							$price = $sid->getPriceLarge();
						}
					}
				}

				$name = $drink->getNameOrId();
			 	$quantity = $drink->getQuantity();

			 	echo "<tr>".
			 		"<td>".$name."</td>".
			 		"<td style=\"width:100px;\"><input type='text' name='quantity' value='".$quantity."' data-provide='typeahead' class='input-mini'></td>".
			 		"<td style=\"width:200px;\">";
					
					if($drink->getScore()){
						echo "$pts Pts";
					}else{
						echo number_format($price, 2, ',', ' ')."$";
					}
					
					echo "</td>".
			 		"<td style=\"width:100px;\"><input type='submit' name='delete' value='Supprimer' class='btn'>".
			 		"</td>".
			             "</tr>";

				$totalCash += ($price*$quantity);
				$totalPts += $pts;
			}
		


			?>
				<tr>
				<td></td>
				<td></td>
				<td colspan='2'><?php echo number_format($totalCash, 2, ',', ' '); ?> $</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td colspan='2'><?php echo $totalPts;?> Pts</td>
			</tr>
		</tbody>
		
		<!-- Footer of the cart : totals and checkout button -->
		<tfoot>			
			<tr>
				<td colspan="4">
					<div class="form-actions">
						<a href="#billing_modal" type="submit" class="btn btn-primary" name="checkout" data-toggle="modal">Checkout</a>
					</div>
					<?php 
						include "billing_page.php";
					?>
				</td>
			</tr>
		</tfoot>
	</table>
</form>
